<?php
use PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Tools\ClassGenerator;
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;
use \PsiFramework\Classes\Util\UI\Printer;

header('content-type: text/html; charset=utf-8');
require_once("../Lib/psiframework.php");
?>
<html>
    <head>
        <style>
            *{font-family:"Courier", "Times New Roman", Times, serif;}
        </style>
    </head>
    <body>
<?php
$request = Request::getInstance();
$objectName = $request->getParameter('object');

if (!isNullOrEmptyString($objectName)) {
    
    $createTableParam = $request->getParameter('table');
    $createTable = ($createTableParam == 'true');
    
    if ($createTable) {
        $objectDao = DAOFactory::getDAO($objectName);
        $objectDao->createTable();
    } else {
        
        $classCode = null;
        $write = ($request->getParameter('write') == 'true');
        
        $type = $request->getParameter('type');
        if (isNullOrEmptyString($type)) {
            $inherit = ($request->getParameter('inherit') == 'true');
            $inherited = ($request->getParameter('inherited') == 'true');
            $classCode = ClassGenerator::getClassCode($objectName, $inherit, $inherited, $write);
        } else if ($type == 'dao') {
            $classCode = ClassGenerator::getDAOClassCode($objectName, $write);
        } else if ($type == 'all') {
            $classCode = '';
            $classCode .= "\n\n".ClassGenerator::getClassCode($objectName, true, false, $write);
            $classCode .= "\n\n".ClassGenerator::getClassCode($objectName, true, true, $write);
            $classCode .= "\n\n".ClassGenerator::getDAOClassCode($objectName, $write);
        }
        
        if (isNullOrEmptyString($classCode)) {
            Printer::display('error', true);
        } else {
            $applicationManager = ApplicationManager::getInstance();
            $application = $applicationManager->getApplication();
            $objectParameters = $application->getObjectParameter($objectName);
            Printer::display($classCode, true);
        }
    }
    
} else {
    Printer::display('no class !');
}
?>
    </body>
</html>